<style type="text/css">
	.border-container::after {
		content: "Editar Produto {{ $product->name }}";
	}
</style>
<div class="container">
	{{ Form::model($product, array('route' => array('product.update', $product->id), 'method' => 'PUT', 'class' => 'form-horizontal', 'role' => 'form')) }}
		<div class="border-container">
			@if (!$errors->isEmpty())
			<div class="alert alert-danger">
				<strong>Ocorreram os seguintes erros:</strong>
				{{ HTML::ul($errors->all()) }}
			</div>
			@endif
			
			<div class="form-group">
				<label for="name" class="col-sm-2 control-label">Nome do Produto </label>
				<div class="col-sm-6">
                	{{ Form::text('name', null, array('class'=>'form-control', 'required'=>'required')) }}
				</div>
            </div>
            
            <h4><span class="sectionTitle">Composição</span></h4>

            <div class="form-group">
            	<div class="col-sm-2 centered">
            		<label>Tipo</label>
            	</div>
            	<div class="col-sm-2 centered">
            		<label>Código do CIGAM</label>
            	</div>
            	<div class="col-sm-1 centered">
            		<label>Qtd</label>
            	</div>
            	<div class="col-sm-4">
            		<label>Nome do Componente</label>
            	</div>
            	<div class="col-sm-3">
            		<label>Ações</label>
            	</div>
            </div>
            
            @foreach ($productComponents as $productComponent)
				<div class="form-group component-group current-component">
					<div class="col-sm-2" style="padding-right: 3px;">
						{{ Form::select('currentAccessories[' . $productComponent->component->id . ']', array('0'=>'Operação','1'=>'Insumo'),  $productComponent->component->accessory, array('class'=>'form-control cboOperationType')) }}
					</div>
					<div class="col-sm-2 col-component">
						<?php
							$currentCigamCodeAttributes = array('class'=>'form-control txtCigamCode', 'required'=>'required', 'autocomplete'=>'off');
							if ($productComponent->component->accessory == 1) {
								$currentCigamCodeAttributes['readonly']='readonly';
							}
						?>
						{{ Form::text('currentCigamCodes[' . $productComponent->component->id . ']', $productComponent->component->code, $currentCigamCodeAttributes) }}
					</div>
					<div class="col-sm-1 col-component">
						{{ Form::text('currentAmounts[' . $productComponent->component->id . ']', $productComponent->amount, array('class'=>'form-control', 'required'=>'required')) }}
					</div>
		  			<div class="col-sm-4 col-component">
		    			{{ Form::text('currentComponents[' . $productComponent->component->id . ']', $productComponent->component->name, array('class'=>'form-control', 'required'=>'required')) }}
		  			</div>
		  			<div class="col-sm-3 col-component">
						{{ Form::hidden('currentCostsCentres[' . $productComponent->component->id . ']', join($productComponent->component->costsCentres()->lists('id'), ',')) }}
		    			<button type="button" class="btn btn-danger btn-sm btnRemoveComponent" data-toggle="tooltip" data-placement="top" title="Remover Componente">
		      				<span class="glyphicon glyphicon-trash"></span>
		    			</button>
		  			</div>
				</div>
			@endforeach
			
			@if (!empty($inputComponents))
	            @foreach ($inputComponents as $key => $item)
					<div class="form-group component-group">
						<div class="col-sm-2" style="padding-right: 3px;">
							<select name="accessories[]" class="form-control cboOperationType">
								<option value="0"@if ($inputAccessories[$key] == 0) selected@endif>Operação</option>
								<option value="1"@if ($inputAccessories[$key] == 1) selected@endif>Insumo</option>
							</select>
						</div>
						<div class="col-sm-2 col-component">
							<input type="text" class="form-control txtCigamCode" name="cigamCodes[]" value="{{ $inputCigamCodes[$key] }}" autocomplete="off" required="required"@if ($inputAccessories[$key] == 1) readonly="readonly"@endif>
						</div>
						<div class="col-sm-1 col-component">
							<input type="text" class="form-control" name="amounts[]" value="{{ $inputAmounts[$key] }}" autocomplete="off" required="required">
						</div>
			  			<div class="col-sm-4 col-component">
			    			<input type="text" class="form-control ui-autocomplete-input" name="components[]" value="{{ $item }}" autocomplete="off" required="required">
			  			</div>
			  			<div class="col-sm-3 col-component">
							<input type="hidden" name="costCentres[]" value="{{ $inputCostCentres[$key] }}">
			    			<button type="button" class="btn btn-danger btn-sm btnRemoveComponent" data-toggle="tooltip" data-placement="top" title="Remover Componente">
			      				<span class="glyphicon glyphicon-trash"></span>
			    			</button>
			  			</div>
					</div>
	            @endforeach
			@endif
			
			<div class="form-group">
				<div class="col-sm-12">
					<button id="btnAddComponent" type="button" class="btn btn-primary">Adicionar componente</button>
				</div>
			</div>
      	</div>
      	
      	{{ Form::submit('Atualizar Produto', array('id' => 'btn-save-product', 'class'=>'btn btn-primary', 'data-loading-text' => 'Aguarde...'))}}
      	
      	{{ HTML::link('product', 'Cancelar', array('class' => 'btn btn-default', 'style'=>'float: right')) }}
      	{{ HTML::link('product/create/' . $product->id,  'Replicar Produto', array('id' => 'btn-copy', 'class'=>'btn btn-info', 'style'=>'float: right; margin-right: 5px'))}}
      	{{ Form::button('Excluir Produto', array('class' => 'btn btn-danger', 'data-toggle' => 'modal', 'data-target' => '#deleteDialog', 'style'=>'float: right; margin-right: 5px;')) }}
 	{{ Form::close() }}
 </div>
<div class="modal" id="deleteDialog" tabindex="-1" role="dialog" aria-labelledby="deleteDialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">Confirmação de exclusão</h4>
			</div>
			<div class="modal-body">
				<p>Este produto será apagado permanentemente.</p>
				<br/> 
       			<b>Atenção!</b> Esta operação não poderá ser desfeita, deseja prosseguir?
			</div>
			<div class="modal-footer">
				{{ Form::button('Fechar', array('class' => 'btn btn-default', 'data-dismiss' => 'modal' )) }}					
				{{ HTML::link('product/' . $product->id, 'Confirmar exclusão', array('class' => 'btn btn-danger', 'data-method' => 'delete', 'rel' => 'nofollow')) }}
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	var allCostsCentres = {{ $costsCentres }};
	
	$(document).ready(function() {
		$("input[name='costCentres[]']").costsCentres({ costsCentres: allCostsCentres });
		$("input[name^='currentCostsCentres']").costsCentres({ costsCentres: allCostsCentres });
/*		$("input[name^='currentComponents']").autocomplete({
			source: function( request, response ) {
				$.ajax({
					url: $.rootUrl + "product/componentsSuggestions",
					data: {term: request.term},
					dataType: "json",
					success: function( data ) {
						response( $.map( data, function( item ) {
							return {
								label: item.code + ' - ' + item.name,
								name: item.name,
								value: item.costs_centres,
								code: item.code,
								isAccessory: item.accessory
							}
						}));
					}
				});
			},
			select: function( event, ui ) {
				var targetComponent = $(this).closest('.form-group');
				this.value = ui.item.name;
				$("select[name='accessories[]']", targetComponent).val(ui.item.isAccessory + "");
				$("input[name='cigamCodes[]']", targetComponent).val(ui.item.code);
				$("input[name='costCentres[]']", targetComponent).val($(ui.item.value).map(function() { return this.id; }).get().join());
				event.preventDefault();
			},
			focus: function (event, ui) {
				this.value = ui.item.name;
				event.preventDefault();
			},
			minLength: 3,
			delay: 200
		});*/
		$(".btnRemoveComponent").tooltip();
	});
</script>